import {
  useEffect,
  useState,
} from 'react'
import {
  useDebounceFn,
  type DebounceOptions,
} from './use-debounce-fn'

export function useDebounce<T>(value: T, options?: DebounceOptions): T {
  const [debounced, setDebounced] = useState(value)

  const { run } = useDebounceFn(() => {
    setDebounced(value)
  }, options)

  useEffect(() => {
    run()
  }, [value])

  return debounced
}
